Msg("====Files module initialized...\n")

function gSAM.GetServerConfigTable(cfg)
	local cfgtable = {}
	for k,v in pairs(file.Find("gSAM/Server/*.txt")) do
		if string.lower(v) == string.lower(cfg) then
			local f = file.Read("gSAM/Server/"..v)
			local t = string.Explode("\n",f)
			for k,v in pairs(t) do
				if v && v != "" then
					if string.Left(v,2) != "//" then
						local l = string.Explode(" - ", v)
						cfgtable[l[1]] = l[2]
					end
				end
			end
		end
	end
	return cfgtable
end

function gSAM.GetServerVar(cfg,key)
	local t = gSAM.GetServerConfigTable(cfg)
	for k,v in pairs(t) do
		if v[key] then
			return v[key]
		end
	end
end

function gSAM.ServerConfigExists(cfg)
	if file.Exists("gSAM/Server/"..cfg..".txt") then
		return true
	end
	return false
end

function gSAM.GetPlayerConfigTable(id)
	local cfgtable = {}
	for k,v in pairs(file.Find("gSAM/Client/*.txt")) do
		if string.lower(v) == string.lower(id) then
			local f = file.Read("gSAM/Client/"..v)
			local t = string.Explode("\n",f)
			for k,v in pairs(t) do
				if v && v != "" then
					local l = string.Explode(" - ",v)
					cfgtable[l[1]] = l[2]
				end
			end
		end
	end
	if cfgtable == {} then
		return false
	else
		return cfgtable	
	end
end

function gSAM.SetPlayerConfigTable(id,t)
	local s = ""
	gSAM.CreatePlayerConfigFile(id)
	if t then
		local s2 = ""
		for k,v in pairs(t) do
			for k2,v2 in pairs(v) do
				if (k2 && k2 != "") && (v2 && v2 != "") then
					s2 = k2.." - "..v2
				end
			end
			s = s.."\n"..s2
		end
		if s && s != "" then
			file.Write("gSAM/Client/"..id..".txt",s)
		end
	end
end

function gSAM.CreatePlayerConfigFile(id)
	if file.Exists("gSAM/Client/"..id..".txt") then
		file.Delete("gSAM/Client/"..id..".txt")
		file.Write("gSAM/Client/"..id..".txt","")
	else
		file.Write("gSAM/Client/"..id..".txt","")
	end
end

function gSAM.GetPlayerVar(id,key)
	local t = gSAM.GetPlayerConfigTable(id)
	for k,v in pairs(t) do
		if v[key] then
			return v[key]
		end
	end
end

function gSAM.SetPlayerVar(id,key,val)
	local t = gSAM.GetPlayerConfigTable(id)
	for k,v in pairs(t) do
		if v[key] then
			v[key] = val
		end
	end
end

function gSAM.PlayerVarExists(id,key)
	local t = gSAM.GetPlayerConfigTable(id)
	for k,v in pairs(t) do
		if v[key] then
			return true
		else
			return false
		end
	end
end

function gSAM.CreatePlayerVar(id,key,val)
	local t = gSAM.GetPlayerConfigTable(id)
	for k,v in pairs(t) do
		if !v[key] then
			v[key] = val
		end
	end
end

function gSAM.RemovePlayerVar(id,key)
	local t = gSAM.GetPlayerConfigTable(id)
	for k,v in pairs(t) do
		if v[key] then
			v[key] = nil
		end
	end
end
